import { Sitting, Running, Jumping, Falling, Rolling, Diving, Hit } from './playerStates.js'
import { Collition } from './collition.js';
export default class Player {
    constructor(game) {
        this.game = game;
        this.width = 100;
        this.height = 91.3;
        this.x = 0;
        this.y = this.game.height - this.height - this.game.groundMargin;
        this.img = document.querySelector('#player')
        this.speedX = 0;
        this.maxSpeedX = 5;
        this.speedY = 0;
        this.G = 1;
        this.frameX = 0;
        this.frameY = 0;
        this.maxFrame = 3;
        this.fps = 20;
        this.timeInterval = 1000 / this.fps;
        this.timer = 0;
        this.states = [new Sitting(this.game, this), new Jumping(this.game, this), new Running(this.game, this),
        new Falling(this.game, this), new Rolling(this.game, this), new Diving(this.game, this), new Hit(this.game, this)];
        this.currentState = this.states[0];
        this.currentState.enter();
    }

    update(input, deltaTime) {
        this.checkCollition()
        this.currentState.handleInput(input)
        //x方向移动
        this.x += this.speedX;
        if (input.includes('ArrowLeft')&&this.currentState!=this.states[6]) this.speedX = -this.maxSpeedX;
        else if (input.includes('ArrowRight')&&this.currentState!=this.states[6]) this.speedX = this.maxSpeedX
        else this.speedX = 0

        //左右边界判断
        if (this.x < 0) this.x = 0;
        else if (this.x > this.game.width - this.width) this.x = this.game.width - this.width

        //y方向移动
        this.y += this.speedY;
        if (!this.onGround()) this.speedY += this.G;
        else this.speedY = 0

        if (this.timer > this.timeInterval) {
            this.frameX > this.maxFrame ? this.frameX = 0 : this.frameX++;
            this.timer = 0;
        } else {
            this.timer += deltaTime;
        }

        if (this.y > this.game.height - this.height - this.game.groundMargin)
            this.y = this.game.height - this.height - this.game.groundMargin

    }
    onGround() {
        return this.y >= this.game.height - this.height - this.game.groundMargin;
    }
    setState(state, speed) {
        this.currentState = this.states[state];
        this.game.speed = this.game.maxSpeed * speed;
        this.currentState.enter()
    }
    draw(context) {
        context.drawImage(this.img, this.width * this.frameX, this.height * this.frameY, this.width, this.height, this.x, this.y, this.width, this.height)
    }
    checkCollition() {
        this.game.enemies.forEach(enemy => {
            if (!(this.x + this.width < enemy.x ||
                this.x > enemy.x + enemy.width ||
                this.y + this.height < enemy.y ||
                this.y > enemy.y + enemy.height)) {
                //collition
                this.game.collitions.push(new Collition(this.game,this.x+this.width*0.5,this.y+this.height*0.5))
                enemy.deleteFlag = true;
                //仅在Rolling/Diving状态可得分，否则眩晕
                if (this.currentState == this.states[4] || this.currentState == this.states[5])
                    this.game.score++;
                else {
                    this.game.heart--;
                    this.setState(6, 0)
                }
            }
        })
    }
}

//1:28